package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.*;
import cn.wolfcode.crm.query.GuardQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IGuardService;
import cn.wolfcode.crm.util.GuardNumBuild;
import cn.wolfcode.crm.util.PageResult;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class GuardServiceImpl implements IGuardService {
    @Autowired
    private GuardMapper guardMapper;
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private OutPayMapper outPayMapper;
    @Autowired
    private PromissGroupMapper promissGroupMapper;

    public void deleteByPrimaryKey(Long id) {
        guardMapper.deleteByPrimaryKey(id);
    }

    public Guard selectByPrimaryKey(Long id) {
        return guardMapper.selectByPrimaryKey(id);
    }

    public List<Guard> selectAll() {
        return guardMapper.selectAll();
    }

    public void saveOrUpdate(Guard entity) {
        if (entity.getId() != null){
            //如果更改了客户姓名,则更改不通过
            if (!guardMapper.selectByPrimaryKey(entity.getId()).getClientName().equals(entity.getClientName())){
                throw new RuntimeException("客户名字不允许修改");
            }
            //汽车对象
            Car car = carMapper.selectByPrimaryKey(entity.getId());
            carMapper.updateByPrimaryKey(entity.getCar());
            entity.setCar(car);
            guardMapper.updateByPrimaryKey(entity);
        }else {
            //新保存的保单
            entity.setState(Guard.STORAGE_STATE);
            entity.setAppDate(new Date());
            Employee employee =(Employee) SecurityUtils.getSubject().getPrincipal();
            entity.setEmployee(employee);
            entity.setEmployeeName(employee.getUsername());
            entity.setNum(GuardNumBuild.getGuardNum());
            //汽车对象的处理
            Car car = carMapper.selectByNumber(entity.getCar().getNumber());
            if (car==null){
                Car car2 = entity.getCar();
                carMapper.insert(car2);
                entity.setCar(car2);
            }else {
                entity.setCar(car);
            }
            //数据库里查询客户,没有这个客户则提示前端去添加
            Client client = clientMapper.selectByName(entity.getClient().getName());
            if (client==null){
                throw new RuntimeException("该客户不存在客户表,请添加");
            }else {
                entity.setClientName(client.getName());
                entity.setClient(client);
            }

            guardMapper.insert(entity);
        }
    }

    public PageResult query(QueryObject qo) {
        //
        int total = guardMapper.query4Count(qo);
        if(total == 0 ){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Guard> rows = guardMapper.query4List(qo);
        return new PageResult(total,rows);

    }
    //提审的service方法
    public void toPending(Long id) {
        if (guardMapper.selectByPrimaryKey(id).getState()!=Guard.UNPAID_STATE){

            guardMapper.updateState(Guard.PENDING_STATE,id);
        }else {
            throw new RuntimeException("已审核通过,请不要重复提交审核");
        }
    }
    //审核拒绝的方法
    public void beRefused(Long id) {
        guardMapper.updateState(Guard.REFUSE_STATE,id);
        Guard guard = guardMapper.selectByPrimaryKey(id);
        Employee employee =(Employee) SecurityUtils.getSubject().getPrincipal();
        guard.setAuditName(employee.getUsername());
        guardMapper.updateAudit(guard);
    }
    //审核通过未付款
    public void unpaid(Long id) {
        guardMapper.updateState(Guard.UNPAID_STATE,id);
        Guard guard = guardMapper.selectByPrimaryKey(id);
        guard.setAuditDate(new Date());
        Employee employee =(Employee) SecurityUtils.getSubject().getPrincipal();
        guard.setAuditName(employee.getUsername());
        guardMapper.updateAudit(guard);
        //插入一单数据到缴费出单表中
        OutPay outPay = new OutPay();
        outPay.setClient(guard.getClient());
        outPay.setGuard(guard);
        outPay.setProduct(guard.getProduct());
        outPay.setPromissgroup(promissGroupMapper.selectByPromiseId(guard.getProduct().getPromise().getId()));
        outPayMapper.insert(outPay);

    }

    //修改状态
    public int updateState(Integer state, Long id){
        return guardMapper.updateState(state,id);
    }

    public Long selectIdByGuardNum(String guardNum) {
        Long guardId = guardMapper.selectIdByGuardNum(guardNum);
        return guardId;
    }

}
